#!/usr/bin/python
# -*- coding: utf-8 -*-

# ESiPC
from esipc import ESiPCProperty

from mikan.view import *
from mikan.core import MIKAN_HTTP_HEADER

class AbstractMikanItemExportView(AbstractMikanView):

	def __init__(self, r):
		super(AbstractMikanItemExportView, self).__init__(r)

		self.FItemAction = None
		self.FItem = None
		self.FItemForm = None

	def set_item(self, value):
		self.FItem = value

	def get_item(self):
		return self.FItem

	def set_item_action(self, action):
		self.item = action.item
		self.itemform = action.itemform
		self.FItemAction = action

	def get_item_action(self, action):
		return self.FItemAction

	def set_item_form(self, value):
		self.FItemForm = value

	def get_item_form(self):
		return self.FItemForm

	def is_item_key(self):
		try:
			if self.request.get_param_str("key", False):
				return True
		except:
			pass
		return False

	def is_item_all(self):
		try:
			if self.request.get_param_str("all", False):
				return True
		except:
			pass
		return False

	# ----------------------------------------------------
	## Property
	item = ESiPCProperty(get_item, set_item, None)
	itemform = ESiPCProperty(get_item_form, set_item_form, None)
	itemaction = ESiPCProperty(get_item_action, set_item_action, None)
	isitemkey = ESiPCProperty(is_item_key, None, None)
	isitemall = ESiPCProperty(is_item_all, None, None)

class AbstractMikanItemsExportView(AbstractMikanView):

	def __init__(self, r):
		super(AbstractMikanItemsExportView, self).__init__(r)

		self.FItems = None
		self.FItemForm = None
		self.FItemAction = None

	def set_items(self, value):
		self.FItems = value

	def get_items(self):
		return self.FItems

	def set_item_action(self, action):
		self.items = action.items
		self.itemform = action.itemform
		self.FItemAction = action

	def get_item_action(self, action):
		return self.FItemAction

	def set_item_form(self, value):
		self.FItemForm = value

	def get_item_form(self):
		return self.FItemForm

	def is_item_key(self):
		try:
			if self.request.get_param_str("key", False):
				return True
		except:
			pass
		return False

	def is_item_all(self):
		try:
			if self.request.get_param_str("all", False):
				return True
		except:
			pass
		return False

	# ----------------------------------------------------
	## Property
	items = ESiPCProperty(get_items, set_items, None)
	itemform = ESiPCProperty(get_item_form, set_item_form, None)
	itemaction = ESiPCProperty(get_item_action, set_item_action, None)
	isitemkey = ESiPCProperty(is_item_key, None, None)
	isitemall = ESiPCProperty(is_item_all, None, None)

class MikanItemXMLView(MikanXMLView, AbstractMikanItemExportView):

	def get_dtd(self):
		return None

	def __init__(self, r):
		super(MikanItemXMLView, self).__init__(r)

	def perform(self):
		f =  None
		if not self.isitemall:
			f = self.itemform

		xml = self.request.db.export("xml", self.item, f)
		self.xml = xml

		# HTTPヘッダーに設定
		self.request.headers[MIKAN_HTTP_HEADER.NAME_CONTENT_DISPOSITION] = MIKAN_HTTP_HEADER.VALUE_CONTENT_DISPOSITION % ("item.xml")

		return True

	def get_content(self):
		c = self.xmlheader + "\n\n"
		if self.dtd <> None:
			c = c + self.dtd + "\n\n"
		if self.xml <> None:
			c = c + str_node(self.xml.documentElement, False) + "\n\n"

		return c.encode("utf-8")

class MikanItemsXMLView(MikanXMLView, AbstractMikanItemsExportView):

	def get_dtd(self):
		return None

	def __init__(self, r):
		super(MikanItemsXMLView, self).__init__(r)

	def perform(self):
		f =  None
		if not self.isitemall:
			f = self.itemform

		xml = self.request.db.exports("xml", self.items, f)
		self.xml = xml

		# HTTPヘッダーに設定
		self.request.headers[MIKAN_HTTP_HEADER.NAME_CONTENT_DISPOSITION] = MIKAN_HTTP_HEADER.VALUE_CONTENT_DISPOSITION % ("items.xml")

		return True

	def get_content(self):
		c = self.xmlheader + "\n\n"
		if self.dtd <> None:
			c = c + self.dtd + "\n\n"
		if self.xml <> None:
			c = c + str_node(self.xml.documentElement, False) + "\n\n"

		return c.encode("utf-8")

class MikanItemCSVView(MikanTextView, AbstractMikanItemExportView):

	def get_content_type(self):
		return "text/csv; charset=" + self.encode

	def set_csv(self, value):
		self.FCSV = value

	def get_csv(self):
		return self.FCSV

	def __init__(self, r):
		super(MikanItemCSVView, self).__init__(r)

	def perform(self):
		f =  None
		if not self.isitemall:
			f = self.itemform

		csv = self.request.db.export("csv", self.item, f, todisplay=(not self.isitemkey))
		self.csv = csv

		# HTTPヘッダーに設定
		self.request.headers[MIKAN_HTTP_HEADER.NAME_CONTENT_DISPOSITION] = MIKAN_HTTP_HEADER.VALUE_CONTENT_DISPOSITION % ("item.csv")

		return True

	def get_content(self):
		c = self.csv
		if c:
			return c.encode("utf_8_sig")
		return ""

	# ----------------------------------------------------
	## Property
	csv = ESiPCProperty(get_csv, set_csv, None)

class MikanItemsCSVView(MikanTextView, AbstractMikanItemsExportView):

	def get_content_type(self):
		return "text/csv; charset=" + self.encode

	def set_csv(self, value):
		self.FCSV = value

	def get_csv(self):
		return self.FCSV

	def __init__(self, r):
		super(MikanItemsCSVView, self).__init__(r)

	def perform(self):
		f =  None
		if not self.isitemall:
			f = self.itemform

		csv = self.request.db.exports("csv", self.items, f, todisplay=(not self.isitemkey))
		self.csv = csv

		# HTTPヘッダーに設定
		self.request.headers[MIKAN_HTTP_HEADER.NAME_CONTENT_DISPOSITION] = MIKAN_HTTP_HEADER.VALUE_CONTENT_DISPOSITION % ("items.csv")

		return True

	def get_content(self):
		c = self.csv
		if c:
			return c.encode("utf_8_sig")
		return ""

	# ----------------------------------------------------
	## Property
	csv = ESiPCProperty(get_csv, set_csv, None)

class MikanItemJSONView(MikanTextView, AbstractMikanItemExportView):

	def set_json(self, value):
		self.FJSON = value

	def get_json(self):
		return self.FJSON

	def get_content_type(self):
		return "application/json; charset=%s" % self.encode

	def __init__(self, r):
		super(MikanItemJSONView, self).__init__(r)

	def perform(self):
		f =  None
		if not self.isitemall:
			f = self.itemform

		json = self.request.db.export("json", self.item, f, todisplay=(not self.isitemkey))
		self.json =json

		# HTTPヘッダーに設定
		self.request.headers[MIKAN_HTTP_HEADER.NAME_CONTENT_DISPOSITION] = MIKAN_HTTP_HEADER.VALUE_CONTENT_DISPOSITION % ("item.json")

		return True

	def get_content(self):
		c = self.json
		if c:
			return c.encode("utf-8")
		return ""

	# ----------------------------------------------------
	## Property
	json = ESiPCProperty(get_json, set_json, None)

class MikanItemsJSONView(MikanTextView, AbstractMikanItemsExportView):

	def set_json(self, value):
		self.FJSON = value

	def get_json(self):
		return self.FJSON

	def get_content_type(self):
		return "application/json; charset=%s" % self.encode

	def __init__(self, r):
		super(MikanItemsJSONView, self).__init__(r)

	def perform(self):
		f =  None
		if not self.isitemall:
			f = self.itemform

		json = self.request.db.exports("json", self.items, f, todisplay=(not self.isitemkey))
		self.json =json

		# HTTPヘッダーに設定
		self.request.headers[MIKAN_HTTP_HEADER.NAME_CONTENT_DISPOSITION] = MIKAN_HTTP_HEADER.VALUE_CONTENT_DISPOSITION % ("items.json")

		return True

	def get_content(self):
		c = self.json
		if c:
			return c.encode("utf-8")
		return ""

	# ----------------------------------------------------
	## Property
	json = ESiPCProperty(get_json, set_json, None)

class MikanItemTextView(MikanTextView, AbstractMikanItemExportView):

	def get_content_type(self):
		return "text/plain; charset=" + self.encode

	def set_text(self, value):
		self.FText = value

	def get_text(self):
		return self.FText

	def __init__(self, r):
		super(MikanItemTextView, self).__init__(r)

	def perform(self):
		f =  None
		if not self.isitemall:
			f = self.itemform

		text = self.request.db.export("text", self.item, f, todisplay=(not self.isitemkey))
		self.logger.debug(text)
		self.text = text

		# HTTPヘッダーに設定
		self.request.headers[MIKAN_HTTP_HEADER.NAME_CONTENT_DISPOSITION] = MIKAN_HTTP_HEADER.VALUE_CONTENT_DISPOSITION % ("item.txt")

		return True

	def get_content(self):
#		return self.text
		return self.text.encode("utf_8")

	# ----------------------------------------------------
	## Property
	text = ESiPCProperty(get_text, set_text, None)
